﻿using System;
using System.Collections.Generic;
using System.Linq;
using HiStaff.Base;
using HiStaff.Control;
using HiStaff.Domain.IN;
using HiStaff.Domain.Sys;
using HiStaff.Util;
using HiStaff.Dal;
using Janus.Windows.GridEX;
using System.Data;
using System.Windows.Forms;

namespace HiStaff.iInsurance
{
    public partial class Summary : FormBase
    {
        private const string DDINS_CENTER = "DDINS_CENTER";
        private const string TCMD_YEAR = "TCMD_YEAR";
        private const string DDMONTH = "DDMONTH";
        private const string DDPERIOD = "DDPERIOD";
        private List<SEARCH> _lstPeriod;
        public Summary()
        {
            InitializeComponent();
        }
        protected override bool FormLoad()
        {
            base.CreateDropdownInCommandBarWithDataList(DDINS_CENTER, "Đơn vị đóng bảo hiểm", 200, "ORG_INS");
            base.CreateTxtInCommandBar(TCMD_YEAR, "Năm", 40, DateTime.Now.Year.ToString());
            base.CreateDropdownInCommandBar(DDMONTH, "Tháng", 35, 12);
            base.CreateDropdownInCommandBar(DDPERIOD, "Đợt", 100, 30);
            base.CreateCommandBar(CommandBarManager.ActionCommand.Calculate,
                CommandBarManager.ActionCommand.Separator,
                CommandBarManager.ActionCommand.Exit);
            BindDropDownToolBar();
            return true;
        }
        protected override bool CalculateButtonClick()
        {

            return cal () ;
        }
        private bool cal()
        {
            try
            {

                var cboInsCenter = base.GetCommandByKey(DDINS_CENTER).GetUIComboBox();
                var cboPeriod = base.GetCommandByKey(DDPERIOD).GetUIComboBox();
                var objSave = new IN_CHANGE();
                objSave.INSCENTERKEY =ConvertHelper.ToDecimal ( cboInsCenter.SelectedValue) ;
                objSave.PERIODREPORT = ConvertHelper.ToDateTime(cboPeriod.SelectedValue);
                OracleHelper.ExcuteNonQuery("PRU_IN_PAYROLL", objSave);
                Load();

                
                return true;
            }
            catch (Exception ex)
            {
                WriteErrorLog(ex);
                return false;
            }
        }
        private void Load()
        {
            var cboInsCenter = base.GetCommandByKey(DDINS_CENTER).GetUIComboBox();
            var cboPeriod = base.GetCommandByKey(DDPERIOD).GetUIComboBox();
            var objSave = new IN_CHANGE();
            var lstInPayRoll = OracleHelper.ExcuteSelectMultiObject<IN_PAYROLL>("PRS_IN_PAYROLL", new SEARCH { INSCENTERKEY = ConvertHelper.ToDecimal(cboInsCenter.SelectedValue), PERIODREPORT = ConvertHelper.ToDateTime(cboPeriod.SelectedValue) });
            jgridData.DataSource = lstInPayRoll;
            jgridData.Refresh();
            var lstInSumary = OracleHelper.ExcuteSelectMultiObject<IN_SUMARY>("PRS_IN_SUMARY", new SEARCH { INSCENTERKEY = ConvertHelper.ToDecimal(cboInsCenter.SelectedValue), PERIODREPORT = ConvertHelper.ToDateTime(cboPeriod.SelectedValue) });
            hiGrid1.DataSource = lstInSumary;
            hiGrid1.Refresh();
            var lstInTotal = OracleHelper.ExcuteSelectMultiObject<IN_SUMARY>("PRS_IN_TOTALSUMARY", new SEARCH { INSCENTERKEY = ConvertHelper.ToDecimal(cboInsCenter.SelectedValue), PERIODREPORT = ConvertHelper.ToDateTime(cboPeriod.SelectedValue) });
            hiGrid2.DataSource = lstInTotal;
            hiGrid2.Refresh();
 
        }
        private void BindDropDownToolBar()
        {
            List<SY_DEFAULT_LIST> lst = new List<SY_DEFAULT_LIST>();
            for (int i = 1; i < 13; i++)
            {
                lst.Add(new SY_DEFAULT_LIST
                {
                    ID = i,
                    NAME = i.ToString("00")
                });
            }
            base.SetDataSourceToDropdownCommand(DDMONTH, lst, "NAME", "ID", true);
        }
        protected override bool CommandValueChanged(Janus.Windows.UI.CommandBars.UICommand Command)
        {
            var cboInsCenter = base.GetCommandByKey(DDINS_CENTER).GetUIComboBox();
            var cboMonth = base.GetCommandByKey(DDMONTH).GetUIComboBox();
            var txtYear = base.GetCommandByKey(TCMD_YEAR).GetTextBox();
            var cboPeriod = base.GetCommandByKey(DDPERIOD).GetUIComboBox();
            decimal val;
            if (decimal.TryParse(txtYear.Text, out val) && txtYear.Text.Length == 4)
            {
                switch (Command.Key)
                {
                    case DDPERIOD:
                        Load();
                        break;
                    default:
                        LoadPeriod(ConvertHelper.ToDecimal(cboInsCenter.SelectedValue), ConvertHelper.ToDecimal(cboMonth.SelectedValue), val);
                        break;
                }
            }

            return true;
        }
        private void LoadPeriod(decimal? InsCenter, decimal? Month, decimal? Year)
        {
            SEARCH objsearch = new SEARCH();

            objsearch.INSCENTERKEY = InsCenter;
            objsearch.MONTH = Month;
            objsearch.YEAR = Year;
            _lstPeriod = OracleHelper.ExcuteSelectMultiObject<SEARCH>("PRS_INSPERIOD", objsearch);
            base.SetDataSourceToDropdownCommand(DDPERIOD, _lstPeriod, "PERIODREPORT_NAME", "PERIODREPORT", true);
        }
    }
}
